<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : UtilService.php
 * Created on : 2019-8-10 15:17:44
 * Author     : Tiger(老虎)
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace haoteam\service;

use app\Request;

/**
 * 综合服务
 * Class UtilService
 * @author Tiger(老虎) 2019-8-10 15:17:44
 */
class UtilService {

    /**
     * 
     * @param type $params
     * @param Request $request
     * @param type $suffix
     * @return type
     */
    public static function postMore($params, Request $request = null, $suffix = false) {
        if ($request === null) {
            $request = app('request');
        }
        $p = [];
        $i = 0;
        foreach ($params as $param) {
            if (is_array($param) == false) {
                $d = $request->post($param);
                if ($d === null && $suffix == false) {
                    continue;
                }
                $p[$suffix == true ? $i++ : $param] = $d;
            } else {
                if (isset($param[1]) == false) {
                    $param[1] = null;
                }
                if (isset($param[3]) == false) {
                    $param[3] = 'strip_tags';
                }
                $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
                $d = $request->post($name, $param[1], $param[3]);
                if ($d === null && $suffix == false) {
                    continue;
                }
                $p[$suffix == true ? $i++ : (isset($param[2]) ? $param[2] : $param[0])] = (is_numeric($d) || is_string($d) ? trim($d) : $d);
            }
        }
        return $p;
    }

    /**
     * 
     * @param type $params
     * @param Request $request
     * @param type $suffix
     * @return type
     */
    public static function getMore($params, Request $request = null, $suffix = false) {
        if ($request === null) {
            $request = app('request');
        }
        $p = [];
        $i = 0;
        foreach ($params as $param) {
            if (is_array($param) == false) {
                $d = $request->get($param);
                if (($d === '' || $d === null) && $suffix == false) {
                    continue;
                }
                $p[$suffix == true ? $i++ : $param] = $d;
            } else {
                if (isset($param[1]) == false) {
                    $param[1] = null;
                }
                if (isset($param[3]) == false) {
                    $param[3] = 'strip_tags';
                }
                $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
                $d = $request->get($name, $param[1], $param[3]);
                if (($d === '' || $d === null) && $suffix == false) {
                    continue;
                }
                $p[$suffix == true ? $i++ : (isset($param[2]) ? $param[2] : $param[0])] = (is_numeric($d) || is_string($d) ? trim($d) : $d);
            }
        }
        return $p;
    }

}
